package pprz.data_components.parsers;

import java.awt.geom.Point2D;
import java.util.Vector;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import pprz.data_components.FlightPlan;
import pprz.data_components.FlightPlan.Block;
import pprz.data_components.FlightPlan.Stage;
import pprz.data_components.FlightPlan.Waypoint;

public class FlightPlanParser extends DefaultHandler {


	String[] StageNames=new String[] {"while","for","exception","heading","attitude",
			"go","xyz","set","call","circle","eight","oval","survey_rectangle",
			"deroute","stay","follow","param","return"};
	

	protected FlightPlan flight_plan;

	protected Block block;
	protected Waypoint waypoint;

	protected Vector<String> blockList;
	
	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		flight_plan=new FlightPlan();
	}

	// SAX Parser Handler methods...
	public void startElement(String namespaceURI, String lName, // local name
			String qName, // qualified name
			Attributes attrs) throws SAXException {
		String eName = lName; // element name
		if ("".equals(eName))
			eName = qName;
		
		if ("flight_plan".equals(eName))
		{
			flight_plan.setAlt(Double.parseDouble(attrs.getValue("alt")));
			flight_plan.setGroundAlt(Double.parseDouble(attrs.getValue("ground_alt")));
			flight_plan.setLat0Lon0(Double.parseDouble(attrs.getValue("lat0")), 
					Double.parseDouble(attrs.getValue("lon0")));
			flight_plan.setMaxDistFromHome(Double.parseDouble(attrs.getValue("max_dist_from_home")));
			flight_plan.setSecurityHeight(Double.parseDouble(attrs.getValue("security_height")));
			flight_plan.setQfu(Double.parseDouble(attrs.getValue("qfu")));
		} else if ("blocks".equals(eName))
		{
			if (blockList == null) blockList=new Vector<String>();
			flight_plan.setBlocks(new Vector<Block>());
			
		} else if ("block".equals(eName))
		{
			blockList.add(attrs.getValue("name"));
			block=flight_plan.getNewBlock();
			block.name=attrs.getValue("name");
			block.stages=new Vector<Stage>();
		} else if ("waypoints".equals(eName))
		{
			flight_plan.setWaypoints(new Vector<Waypoint>());
		} else if ("waypoint".equals(eName))
		{
			waypoint=flight_plan.getNewWaypoint();
			waypoint.name=attrs.getValue("name");
			waypoint.location=new Point2D.Double(Double.parseDouble(attrs.getValue("x")),
					Double.parseDouble(attrs.getValue("y")));
				String alt_attr = attrs.getValue("alt");
			if (alt_attr!=null) waypoint.altitude=Double.parseDouble(alt_attr);
		} else {
			for (String item : StageNames){ 
				   if (item.equals(eName)) {
					   //StringBuffer s=new StringBuffer(eName);
					   Stage s=flight_plan.getNewStage(eName);
					   for (int i=0; i<attrs.getLength(); i++)
					   {s.att.names.add(attrs.getQName(i)); s.att.vals.add(attrs.getValue(i)); }
					   //s.append(":").append(attrs.toString());
					   block.stages.add(s);
				   }
			}
		}
	}

	public void endElement(String namespaceURI, String sName, // simple name
			String qName // qualified name
	) throws SAXException 
	{
		String eName = sName; // element name
		if ("".equals(eName))
			eName = qName;
		if ("blocks".equals(eName)) {
			
		} else if ("waypoints".equals(eName)) {
			
		} else if ("waypoint".equals(eName)) {
			flight_plan.getWaypoints().add(waypoint);
		} else if ("block".equals(eName)) {
			flight_plan.getBlocks().add(block);
		}
	}

	public void characters(char buf[], int offset, int len)
			throws SAXException {	}
	
	public Vector<String> get_blockList()
	{return blockList;}

	public FlightPlan getFlightPlan() {
		return flight_plan;
	}
	
	
	
	
	
}
